package com.example.android.plugin.host;

import com.example.android.plugin.framework.beans.PluginFeatureMethod;
import com.example.android.plugin.framework.beans.PluginIntent;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PluginItem extends LinearLayout {

	private Context context;
	private TextView title;
	private TextView description;
	private LinearLayout features;
	private String[] dialogItems = null;
	private int flag = -1;
	private String pkgName;

	public PluginItem(final Context context, final String pkgName) {
		super(context);
		this.context = context;
		this.pkgName = pkgName;

		LayoutInflater layoutinflater = LayoutInflater.from(context);
		View view = layoutinflater.inflate(R.layout.plugin_item, this, true);

		title = (TextView) view.findViewById(R.id.plugin_item_title);
		description = (TextView) view
				.findViewById(R.id.plugin_item_description);
		features = (LinearLayout) view.findViewById(R.id.plugin_item_features);

		dialogItems = new String[] { context.getString(R.string.delete_plugin) };
		this.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				new AlertDialog.Builder(context)
						.setTitle(R.string.choose)
						.setNegativeButton(R.string.cancel,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										dialog.dismiss();
									}
								})
						.setPositiveButton(R.string.confirm,
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										if (0 == flag) {
											Uri packageURI = Uri
													.parse("package:" + pkgName);
											Intent uninstallIntent = new Intent(
													Intent.ACTION_DELETE,
													packageURI);
											context.startActivity(uninstallIntent);
										}
									}
								})
						.setSingleChoiceItems(dialogItems, -1,
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int item) {
										flag = item;
									}
								}).show();
				return false;
			}
		});
	}

	public void setIconImage(Drawable drawable) {
		drawable.setBounds(0, 0, 64, 64);
		title.setCompoundDrawables(drawable, null, null, null);
	}

	public void setDescription(String text) {
		description.setText(text);
	}

	public void setTitle(String text) {
		title.setText(text);
	}

	public void addPluginMethod(PluginFeatureMethod method, OnClickListener ocl) {
		Button btn = new Button(context);
		btn.setText(method.getDescription());
		btn.setOnClickListener(ocl);
		features.addView(btn);
	}

	public void addPluginMethod(PluginIntent intent, OnClickListener ocl) {
		Button btn = new Button(context);
		btn.setText(intent.getDescription());
		btn.setOnClickListener(ocl);
		features.addView(btn);
	}

	public LinearLayout getFeatures() {
		return features;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public String getPkgName() {
		return pkgName;
	}

	public void setPkgName(String pkgName) {
		this.pkgName = pkgName;
	}
}
